问题
线上遇到bug,写了定时器,展示打卡时间读秒,结果定时器越跑延迟越大,用setTimeout fix了。记录下
1 2 3 4 5 6 7 8 9 10 11 12 13
| var startTime = new Date().getTime(); var count = 0; function handle () { count++; var offset = new Date().getTime() - (startTime + count * timeRange); var nextTime = timeRange - offset; if (nextTime < 0) nextTime = 0; timer = setTimeout(handle, nextTime); totalDiatane += distanceBlock document.getElementById('distance').innerText = totalDiatane.toFixed(2) } timer = setTimeout(handle, timeRange);
|